package com.example.demo01.fragments;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo01.R;
import com.example.demo01.utils.Utils;

import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TitleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TitleFragment extends Fragment {
    private String title;

    private ImageView iv_title_back;
    private TextView tv_title_title;

    public static TitleFragment newInstance(String title) {

        Bundle args = new Bundle();
        args.putString("title", title);
        TitleFragment fragment = new TitleFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString("title");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_title, container, false);

        initView(view);

        return view;
    }

    //. 初始化视图
    private void initView(View v) {
        iv_title_back = v.findViewById(R.id.iv_title_back);
        tv_title_title = v.findViewById(R.id.tv_title_title);
        if (title != null) tv_title_title.setText(title);
        addListener();
    }

    //. 添加点击事件
    private void addListener() {
        iv_title_back.setOnClickListener(v -> {
            requireActivity().finish();
        });
    }
}